var express = require('express');
var router = express.Router();
var mysql = require('./mysqlConfig');
var bodyParser = require("body-parser");
// var redis = require('./redisServer');
var register = require('babel-core/register')
var uuid = require('node-uuid')
router.use(bodyParser.json({
    limit: "20000kb"
}));
router.use(bodyParser.urlencoded({
    parameterLimit: 100000,
    limit: '20000kb',
    extended: true
}));

/* GET home page. */

//处理连接数据库
const connectionMysql = (sql) => {
    return new Promise(function(resolve, reject) {
        mysql.getConnection(function(err, connection) {
            if (err) {
                reject(err)
                console.log(err)
            } else {
                resolve(sqlSever(connection, sql))
            }
        });
    });
}
//处理sql语句
const sqlSever = (connection, sql) => {
    return new Promise(function(resolve, reject) {
        connection.query(sql, function(err, rows, fields) {
            if (err) {
                console.log(err)
                reject(err);
            } else {
                console.log(err)
                resolve(rows);
                connection.release();
            }
        })
    });
}

//获取最新活动列表
router.get('/getActivity', function(req, res, next) {
    var num = "'" + req.query.num + "'";
    var sql = 'select  ac.* , m.title,m.acti_no from event_registration ac left join activities m on m.acti_no= ac.acti_no where mem_id = ' + num;
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});
//获取我的关注
router.get('/getFocus', function(req, res, next) {
    var num = "'" + req.query.num + "'";
    var sql = 'select  ac.* , m.* from mem_collection ac left join expert m on m.expert_no= ac.acti_no where mem_id = ' + num;
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});
//获取我的点赞
router.get('/getLike', function(req, res, next) {
    var num = "'" + req.query.num + "'";
    var sql = 'SELECT m.TITLE,m.ARTICLE_ID  FROM mem_collection ac LEFT JOIN cms_article m ON m.ARTICLE_ID = ac.acti_no WHERE mem_id =' + num;
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});


module.exports = router;